﻿using System;
using System.Xml.Serialization;
using TI;
using TI_EntityLib.TiEntityObjects;
using TiPort.TiEntityObjects;
using TiEntityObject.Notification.MergeTI.TiEntityObjects;
using TI_EntityLib.TiEntityObjects.AttachInfo;

namespace TiEntityObject.Notification
{
    public class Notification : MongoEntity, IDbRootObject 
    {
        
        [XmlElement("id")]
        public Int64  ID { get; set; }

        [XmlElement("notificationNumber")]
        public string NotificationNumber { get; set; }

        [XmlElement("foundationNotificationNumber")]
        public string FoundationNotificationNumber { get; set; }

        [XmlElement("versionNumber")]
        public int VersionNumber { get; set; }
        
        [XmlElement("createDate")]
        public string CreateDate { get; set; }
        public long iCreateDate { get { return Helper.Time.GetDateInMS(CreateDate ); } }

        [XmlElement("publishDate")]
        public string PublishDate { get; set; }
        public long iPublishDate { get { return Helper.Time.GetDateInMS(PublishDate); } }

        [XmlElement("href")]
        public string Href { get; set; }

        [XmlElement("placingWay")]
        [LinkObjectAttribute(ChildFieldName = "code", IsDictionaryValue = false,IsPartialObject =false , ParentFieldName = "PlacingWayID")]
        public NotificationPlacingWay PlacingWay { get; set; }


        [XmlElement("EP")]
        [LinkObjectAttribute(ChildFieldName = "Code", IsDictionaryValue = false, IsPartialObject = false, ParentFieldName = "EpID")]
        public NotificationEP EP { get; set; }


        [XmlElement("orderName")]
        public string OrderName { get; set; }

        [XmlElement("order")]
        [LinkObjectAttribute(ChildFieldName = "ID", IsPartialObject = true, ParentFieldName = "ID")]
        public NotificationOrder Order { get; set; }

        [XmlElement("contactInfo")]
        [LinkObjectAttribute(ChildFieldName = "NotificationID", IsPartialObject = true, ParentFieldName = "ID")]
        public NotificationContactInfo ContactInfo { get; set; }

        [XmlElement("printForm")]
        [LinkObjectAttribute(ChildFieldName = "NotificationID", IsPartialObject = true, ParentFieldName = "ID")]
        public NotificationPrintForm PrintForm { get; set; }

        [XmlElement("documentMetas")]
        [LinkObjectAttribute(ChildFieldName = "NotificationID", IsCollection = true, IsPartialObject = true, ParentFieldName = "ID")]
        public NotificationDocumentMetas DocumentMetas { get; set; }
      

        [XmlElement("lots")]
        [LinkObjectAttribute(ChildFieldName = "NotificationID", IsCollection = true, IsPartialObject = true, ParentFieldName = "ID")]
        public NotificationLots Lots { get; set; }

        [XmlElement("competitiveDocumentProvisioning")]
        [LinkObjectAttribute(ChildFieldName = "NotificationID", IsPartialObject = true, ParentFieldName = "ID")]
        public NotificationCompetitiveDocumentProvisioning CompetitiveDocumentProvisioning { get; set; }

        [XmlElement("notificationCommission")]
        [LinkObjectAttribute(ChildFieldName = "ID", IsPartialObject = true, ParentFieldName = "ID")]
        public NotificationComission NotificationCommission { get; set; }

        [XmlElement("NotifictionModification")]
        public NotifictionModification NotifictionModification { get; set; }


        [XmlElement("notificationPlacement")]
        public NotificationPlacement NotificationPlacement { get; set; }

        public decimal MaxPrice {
            get {
                
                decimal r = 0;
                try
                {
                    foreach (NotificationLot l in this.Lots.LotList)
                    {
                        r = r + l.LotMaxPrice;
                    }
                }
                catch {
                    return 0;
                }
                return r;
            }
        }

        public  DateTime LastUpdateDate
        {
            get
            {
                return DateTime.Now;
            }
            set
            {
                ;
            }
        }
    
        public long iLastUpdateDate { get { return Helper.Time.GetDateInMS(this.LastUpdateDate); } }

        public override string GetCollectionName()
        {
            return "Notification";
        }

        public override string GetAsJson()
        {
            return Helper.Json<Notification>.NewtonSerializeObject(this);
        }
     
        public override string GetInsertScript()
        {
            string v = GetAsJson();            
            return "mergeNotification("+v+")";
        }

        public string Key
        {
            get { return ID.ToString(); } 
          
        }

        public long iFileDate
        {
            get;
            set;
        }

        public string FileName
        {
            get;
            set;
        }

        public string ObjectType
        {
            get;
            set;
        }

        public TiEntityClass Class
        {
            get
            {
                try
                {
                    if (this.OrderName.ToUpper().IndexOf("ЛЕКАРСТВЕ") > -1)
                        return TiEntityClass.Pharm;


                    if (this.OrderName.ToUpper().IndexOf("МЕДИКАМ") > -1)
                        return TiEntityClass.Pharm;

                    if (this.OrderName.ToUpper().IndexOf("МЕДИЦИН") > -1)
                        return TiEntityClass.Pharm;

                    foreach (NotificationLot l in this.Lots.LotList)
                    {
                        foreach (NotificationLotProduct p in l.Products.ProductColl)
                        {
                            if (p.lCode > 2423000 & p.lCode < 2423999)
                                return TiEntityClass.Pharm;
                        }
                    }

                }
                catch
                {
                    return TiEntityClass.None;
                }

                return TiEntityClass.None;
            }
        }
        
        public long iTransportDay
        {
            get;
            set;
        }

        public TI_DATA TI_DATA { get; set; }
    }
}
